import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const routes = [{
  path: "/",
  redirect: "/login"
}, {
  path: '/login',
  name: 'Login',
  component: () => import('../components/login.vue')
}, {
  path: '/home',
  name: 'Home',
  // 页面发生跳转步骤原来 先把home组件展示出来然后重定向到weclome组件来
  redirect: "/weclome",
  component: () => import('../components/home.vue'),
  children: [{
    path: "/weclome",
    name: 'Weclome',
    component: () => import('../components/weclome.vue'),
  }, {
    path: "/user",
    name: 'User',
    component: () => import('../components/user/user.vue'),
  }, {
    path: "/rights",
    name: 'Rights',
    component: () => import('../components/power/rights.vue'),
  }, {
    path: "/roles",
    name: 'Roles',
    component: () => import('../components/power/roles.vue'),
  }, {
    path: "/cate",
    name: 'Cate',
    component: () => import('../components/goods/cate.vue'),
  }]
}];
const router = new VueRouter({
  routes
});
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to要访问的路径
  // from 代表从那个路径跳转过来
  // next 放行 next("/login"); 强制跳转
  if (to.path == "/login") return next();
  // 获取token
  const token = window.sessionStorage.getItem("token");
  // 如果没有token值，强制跳转到登录页面
  if (!token) return next("/login");
  // 如果token有值，直接放行吧
  next();
});
export default router